package com.tom.springboot3demo;

import com.tom.springboot3demo.constent.UrlContent;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringJoiner;

public class ExcelCharacterFontColorExample {
    public static void main(String[] args) {
        String excelFilePath = UrlContent.test1_url;

        try (FileInputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
            Row row = sheet.getRow(0);



            // 获取第一行
            Cell cell = row.getCell(0); // 获取第一个单元格

            if (cell != null && cell.getCellType() == CellType.STRING) {
                //每一个单元格里面的内容是富文本
                XSSFRichTextString richTextString = (XSSFRichTextString) cell.getRichStringCellValue();
                StringJoiner sb = new StringJoiner("");
                for (int i = 0; i < richTextString.numFormattingRuns(); i++) {
                    int startIdx = richTextString.getIndexOfFormattingRun(i);
                    int endIdx = richTextString.getIndexOfFormattingRun(i + 1);
                    if (i == richTextString.numFormattingRuns() - 1) {
                        endIdx = richTextString.length();
                    }
                    XSSFFont xssfFont = richTextString.getFontAtIndex(startIdx);
                    XSSFColor color = xssfFont.getXSSFColor();
                    String textSegment = richTextString.getString().substring(startIdx, endIdx);
                    if (color != null) {
                        byte[] rgb = color.getRGB();
                        if (rgb != null) {
                            int red = rgb[0] & 0xFF;
                            int green = rgb[1] & 0xFF;
                            int blue = rgb[2] & 0xFF;
                            //System.out.println("Text Segment: \"" + textSegment + "\" - Font Color: RGB(" + red + ", " + green + ", " + blue + ")");

                            /*rgb 红色*/
                            /*color: rgb(255, 0, 0);*/

                            if(red == 255&&green==0&&blue ==0){
                                sb.add("<span style=\"color:red\">"+textSegment+"</span>");
                                //System.out.println("<span style=\"color:red\">"+textSegment+"</span>");
                            }else {
                                sb.add("<span>"+textSegment+"</span>");
                                //System.out.println("<span>"+textSegment+"</span>");
                            }
                            /*rgb 黑色*/
                            /*color: rgb(0,0,0);*/

                        } else {
                            //System.out.println("Text Segment: \"" + textSegment + "\" - Font Color: Default");
                            sb.add("<span>"+textSegment+"</span>");
                            //System.out.println("<span>"+textSegment+"</span>");
                        }
                    } else {
                        //System.out.println("Text Segment: \"" + textSegment + "\" - Font Color: Default");
                        //System.out.println("<span>"+textSegment+"</span>");
                        sb.add("<span>"+textSegment+"</span>");
                    }
                }
                System.out.println(sb.toString());
            } else {
                System.out.println("Cell is empty or not a string");
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
